package com.bolt.admin.web.controller;

import com.bolt.admin.common.base.BaseController;
import com.bolt.admin.logging.annotation.LogAnnotation;
import com.bolt.admin.module.sys.dto.MenuDTO;
import com.bolt.admin.module.sys.dto.converter.MenuConverter;
import com.bolt.admin.module.sys.entity.MenuEntity;
import com.bolt.admin.module.sys.service.MenuService;
import com.bolt.common.reflect.ObjectUtil;
import com.bolt.common.utils.StrUtil;
import com.bolt.convention.data.ResultMessage;
import com.bolt.convention.data.Returns;
import com.bolt.core.view.components.Form;
import com.bolt.support.ui.tree.vo.UITreeNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 菜单Controller
 * Created by mi on 2017/9/29.
 */
@Controller
@RequestMapping("/admin/sys/menu")//请修改根url
public class MenuController extends BaseController <MenuEntity, Long> {

    @Autowired
    private MenuService menuService;

    /**
     * 模块首页
     */
    @RequestMapping(value = "", method = RequestMethod.GET)
    public ModelAndView index(Model model, HttpServletRequest request) {
        return super.index("/admin/sys/menu/index", request, model.asMap());
    }

    /**
     * 菜单新增操作
     *
     * @return
     */
    @RequestMapping(value = "create", method = RequestMethod.GET)
    public String create() {
        return "/admin/sys/menu/input-index";
    }

    /**
     * 菜单编辑页
     *
     * @return
     */
    @RequestMapping(value = "edit", method = RequestMethod.GET)
    public String edit(@RequestParam("id") Long id, Model model) {
        MenuEntity menuEntity = menuService.findOne(id).get();
        model.addAttribute(Form.PAGECONTEXT_FORMVALUES, MenuConverter.toDTO(menuEntity));
        return "/admin/sys/menu/input-index";
    }

    /**
     * 菜单编辑操作
     *
     * @param dto 表单数据
     * @return
     */
    @RequestMapping(value = "saveOrUpdate", method = RequestMethod.POST)
    @ResponseBody
    @LogAnnotation(module = "系统管理",description ="菜单编辑操作")
    public ResultMessage saveOrUpdate(@Validated  MenuDTO dto) {
        menuService.saveOrUpdate(dto);
        return Returns.success("保存成功");
    }

    /**
     * 菜单删除操作
     *
     * @param id 菜单ID
     * @return
     */
    @RequestMapping(value = "del", method = RequestMethod.GET)
    @ResponseBody
    @LogAnnotation(module = "系统管理",description ="菜单删除操作")
    public ResultMessage doDelete(@RequestParam("id") Long id) {
        if (ObjectUtil.isNotNull(id)) {
            menuService.deleteById(id);
            return Returns.success();
        } else {
            return Returns.error("所有选取记录删除操作失败");
        }
    }


    @RequestMapping(value = "getMenuList")
    @ResponseBody
    public ResultMessage getMenuList() {
        return Returns.success(menuService.findAllCached());
    }

    /**
     * 取出菜单树形
     *
     * @param id     查询节点ID
     * @param params 　查询扩展参
     * @return
     */
    @RequestMapping(value = "getTree")
    @ResponseBody
    public List <UITreeNode> getTree(@RequestParam(name = "id", required = false) String id, @RequestParam Map <String, String> params) {
        boolean loadAllNodes = StrUtil.isEmpty(params.get("loadAllNodes")) ? false : Boolean.parseBoolean(params.get("loadAllNodes"));
        return super.getTreeNodes(menuService, id, loadAllNodes, params);
    }


    /**
     * 根据菜单生成生权限资 源
     *
     * @param menuId
     * @return
     */
    @RequestMapping("importPrivilege")
    @ResponseBody
    public ResultMessage importPrivilege(@RequestParam(value = "menuId", required = false) Long menuId) {
        menuService.impMenu2Privilege(menuId);
        return Returns.success("导入成功");
    }
}
